HasManyThrough.getValueByParentKey   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import Relation from "../Relation";
2
import {ModelStaticInterface} from "../../../JeloquentInterfaces";
3
import Collection from "../../Collection";
4
import ForeignKey from "../Field/ForeignKey";
5
6
/**
7
 *
8
 */
9
export default class HasManyThrough extends Relation {
10
11
    localKey: string;
12
13
    throughModel: ModelStaticInterface;
14
15
    private _lcModelClassName: string;
16
17
    private _lcParentClassName: string;
18
19
    private _lcThroughModelClassName: string;
20
21
    constructor(model: ModelStaticInterface, throughModel: ModelStaticInterface, foreignKey: string = null, localKey: string = null) {
22
        super(model, foreignKey);
23
        this.model = model;
24
        this.throughModel = throughModel;
25
        this.localKey = localKey ?? 'id';
26
    }
27
28
    get indexName(): string {
29
        return `${this._lcThroughModelClassName}.${this._lcParentClassName}_id`;
30
    }
31
32
    get originalValue(): Collection {
33
        return this.getValueByParentKey('originalPrimaryKey');
34
    }
35
36
    get value(): Collection {
37
        return this.getValueByParentKey('primaryKey');
38
    }
39
40
    getRelationalFields():Array<ForeignKey> {
41
        return [];
42
    }
43
44
    setName() {
45
        this._lcThroughModelClassName = this.throughModel.snakeCaseClassName;
46
        this._lcModelClassName = this.model.snakeCaseClassName;
47
        this._lcParentClassName = this.$parent.snakeCaseClassName;
48
        this.foreignKey = `${this._lcThroughModelClassName}_id`;
49
        this.$name = `${this._lcModelClassName}s`;
50
        return this;
51
    }
52
53
    tableSetup() {
54
        this.model.registerIndex(this.indexName);
55
    }
56
57
    private getValueByParentKey(parentProperty: string): Collection {
58
        const keyIndex = this.model.getIndexByKey(this.indexName);
59
        return globalThis.Store.database().find(this.model.className,
60
            [...(keyIndex.get(`${this.$parent[parentProperty]}`)?.values()) ?? []]
61
        );
62
    }
63
}